$(function () {

    //抽取变量
    var payManage_form = $("#payManage_form");
    var payManage_dialog = $("#payManage_dialog");
    var payManage_datagrid = $("#payManage_datagrid");
    var btn_changeState = $("#btn_changeState");

    //所有a标签的方法都交给methodObject对象来管理
    var methodObject = {
        add: function () {

            payManage_form.form("clear");
            payManage_dialog.dialog("setTitle", "员工新增");
            payManage_dialog.dialog("open");
        },
        edit: function () {


            var payManage = payManage_datagrid.datagrid("getSelected");

            if (!payManage) {
                $.messager.alert('温馨提示', '请选中一条记录！', 'info');
                return;
            }

            payManage_form.form("clear");
            payManage_dialog.dialog("setTitle", "员工编辑");

            if (payManage.payer) {
                payManage["payer.id"] = payManage.payer.id;
            }
            if (payManage.employee) {
                payManage["employee.id"] = payManage.employee.id;
            }
            if (payManage.handler) {
                payManage["payment.id"] = payManage.payment.id;
            }
            payManage_form.form("load", payManage);

            //进行角色的数据回显,无法通过同名匹配,因为查询出来的为多个对象,不能通过payManage.roles.id来匹配,需要通过combobox的setValue向里面传入一个对应id的集合


            payManage_dialog.dialog("open");
        },
        reload: function () {
            $("#ss").searchbox("clear");
            $("#btn_classgrade").combobox("clear");
            $("#btn_beginDate").datebox("clear");
            $("#btn_endDate").datebox("clear");
            $.get("/payManage/query.do", function (data) {

                payManage_datagrid.datagrid("loadData", data);
            })
        },
        save: function () {
            payManage_form.form("submit", {
                url: "/payManage/saveOrUpdate.do",

                success: function (data) {
                    var data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.message, "info", function () {
                            payManage_dialog.dialog("close");
                            payManage_datagrid.datagrid("load");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.message, "info");
                    }
                }
            })
        },
        cancel: function () {
            payManage_dialog.dialog("load");
        },
        delete: function () {
            var payManage = payManage_datagrid.datagrid("getSelected");
            if (!payManage) {
                $.messager.alert("温馨提示", "请选择一条数据", "info");
                return;
            }
            $.messager.confirm("确认窗口", "确认要删除吗", function (r) {
                if (r) {
                    var id = payManage_datagrid.datagrid("getSelected").id;
                    $.get("/payManage/delete.do", {id: id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", data.message, "info", function () {
                                payManage_datagrid.datagrid("reload");
                            })
                        } else {
                            $.messager.alert("温馨提示", data.message, "info");
                        }
                    })
                }
            })
        },
        changeState: function () {
            var row = payManage_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定执行该操作吗？', function (r) {
                if (r) {
                    $.get("/payManage/changeState.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功', 'info')
                            payManage_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        reload: function () {
            payManage_datagrid.datagrid('reload');
        },
        searchForEmp: function () {
            var keyword = $("input[name = 'keyword']").val();
            search(keyword);
        },
        exportXls: function () {
            //xls文件导出
            window.location.href = "/payManage/exportXls.do";
        },
        importXls: function () {
            //xls文件导入
            $("#payManage_import").dialog("open");
        },
        download: function () {
            //文件下载
            window.location.href = "/payManage/download.do";
        }
    };

    //所有a标签的点击事件对应的方法都交给methodObject对象来管理,调用的时候只需要获取对应的cmd质量,然后通过method对象来调用即可
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObject[cmd]();
    });

    //初始化datagrid
    payManage_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        striped: true,
        url: "/payManage/query.do",
        pagination: true,
        singleSelect: true,
        rownumbers: true,
        toolbar: "#payManage_toolbar",
        columns: [[
            {field: 'time', title: '时间', width: 100, align: 'center'},
            {field: 'money', title: '支付金额', width: 100, align: 'center'},
            {field: 'summary', title: '摘要', width: 100, align: 'center'},
            {
                field: 'payer', title: '支付人', width: 100, formatter: function (value) {
                return value ? value.username : "";
            }
            },
            {
                field: 'handler', title: '经手人', width: 100, formatter: function (value) {
                return value ? value.username : "";
            }
            },
            {
                field: 'payment', title: '支付方式', width: 100, formatter: function (value) {
                return value ? value.name : "";
            }
            },
            {
                field: 'pay_type', title: '花费类型', width: 100, align: 'center'
            },
            {field: 'bill_number', title: '单据号', width: 100, align: 'center'},
            {
                field: 'classMan', title: '归属班级', width: 100, formatter: function (value) {
                return value ? value.className : "";
            }
            },
            {
                field: 'employee', title: '审核人', width: 100, formatter: function (value) {
                return value ? value.username : "";
            }
            },
            /*{
             field: 'deptSn', title: '部门编号', width: 100, align: 'center', formatter: function (value, row, index) {
             return row.department ? row.department.sn : "";
             }
             },*/

            {
                field: 'state', title: '状态', width: 100, align: 'center', formatter: function (value, row, index) {

                return value ? "已审核" : "<font color='red'>未审核</font>"

            }
            }
        ]],
        onSelect: function (index, data) {
            if (!data.state) {
                /*btn_changeState.linkbutton("disable");*/
                //选中判断员工状态设置按钮文本显示离职还是在职
                btn_changeState.linkbutton({
                    text: "审核",
                    iconCls: "icon-audit",
                })
                $("#btn_edit").linkbutton({

                        text: "编辑",
                        iconCls: "icon-edit",

                    }
                )
                $("#btn_delete").linkbutton({

                        text: "删除",
                        iconCls: "icon-remove",

                    }
                )

                //移除data-cmd属性,同时增加disable方法禁用按钮
                $("a[data-cmd='edit']").data("cmd", "edit");
                $("a[data-cmd='edit']").linkbutton("enable");
                $("a[data-cmd='delete']").data("cmd", "delete");
                $("a[data-cmd='delete']").linkbutton("enable");

            } else {
                //btn_changeState.linkbutton("enable");
                btn_changeState.linkbutton({
                    text: "取消审核",
                    iconCls: "icon-unaudit",
                })
                $("#btn_delete").linkbutton({

                    text: "",
                    iconCls: null,
                    disabled: true
                })
                $("#btn_edit").linkbutton({

                        text: "",
                        iconCls: null,
                        disabled: true
                    }
                );
                //同时设置编辑按钮为不可编辑

                $("a[data-cmd='edit']").data("cmd", "");
                $("a[data-cmd='edit']").linkbutton("disable");

                $("a[data-cmd='delete']").data("cmd", "");
                $("a[data-cmd='delete']").linkbutton("disable");

            }
        }
    });

    //初始化dialog
    payManage_dialog.dialog({
        width: 400,
        height: 500,
        buttons: "#payManage_btns",
        closed: true
    });

    //初始化一个文件上传的dialog
    $("#payManage_import").dialog({
        width: 400,
        height: 200,
        closed: true,
        title: "文件上传"
    })

});

//注意,searchbox指定的search方法不能抽到method中管理
function search(value) {
    var pay_type = $("input[name = 'pay_type']").val();
    var beginDate = $("input[name = 'beginDate']").val();
    var endDate = $("input[name = 'endDate']").val();

    $("#payManage_datagrid").datagrid("load", {
        keyword: value,
        pay_type: pay_type,
        beginDate: beginDate,
        endDate: endDate
    });

}